<?php

    //Alexander Cam Liu & Justin Lee

    session_start();
    include 'mysqlfunctions.php';

    //Get all the values from the form
    $book = $_POST['book'];
    $author = $_POST['author'];
    $issn = $_POST['issn'];
    $edition = $_POST['edition'];
    $class = $_POST['class'];
    $professor = $_POST['professor'];
    $price = $_POST['price'];
    $description = $_POST['description'];
    $image = $_POST['image'];
    
    

    //If the non-required fields are not filled, set as "NOT PROVIDED"
    if($issn == "") {
        $issn = "NOT PROVIDED";
    }

    if($author == "") {
        $author = "NOT PROVIDED";
    }

    if($edition == "") {
        $edition = "NOT PROVIDED";
    }

    if($professor == "") {
        $professor = "NOT PROVIDED";
    }
    if($image == "") {
        $image = "NOT PROVIDED";
    }    
    //Here we are counting all files that has the same book name. 
    //That way when creating dynamically a new file, it does override any of the old ones. 
    //So if Data Structures book name is not found in the books folder, then the new file will be DataStructures0.php
    //Then if someone adds the same book, the new one file link will be DataStructures1.php
    //And so on
    
    $num_files = glob("books/" . $book . "*.php");

    if($num_files == "") {

    $ourFileName = $book . ".php";
  
    }

    else {

    $ourFileName = $book . count($num_files) .".php";

   }
    
    $ourFileHandle = fopen("books/" .$ourFileName, 'w') or die("can't open file");

    addBook($_SESSION['myusername'],$book, $issn, $edition, $class, $professor, $price, $ourFileName, $author);
    
    $data = 

    //Justin Lee & Alexander Cam Liu

    '<?php
    include \'../mysqlfunctions.php\'; 
    session_start();

    ?>
    
        <!DOCTYPE html>
        <html>
        <head>
            <title>' .$book . '</title>
         <!-- Bootstrap -->
            <link href="../css/bootstrap.css" rel="stylesheet">
            <script src="http://code.jquery.com/jquery-latest.js"></script>
            <script src="../js/bootstrap.min.js"></script>
            <link href="../css/book.css" rel="stylesheet">
            <link href="../css/docs.css" rel="stylesheet">
            <script src="../js/infogenerate.js"></script>
        </head>
        <body style="background-color:#FAFAFA">
            <div class="navbar navbar-fixed-top">
              <div class="navbar-inner">
                <div class="container-fluid">
                    <?php if (isset($_SESSION[\'myusername\'])) {
                    ?>
                    <a class="brand" href="#"><?php echo $_SESSION[\'myusername\'] ?></a>
                    <div class="nav-collapse collapse">
                        <ul class="nav">
                        <li><a href="../logout.php">Logout</a></li>
                        </ul>
                    </div>
                    <?php } ?>
                </div>
              </div>
            </div>
            <br>
            <div class="container">
                <center><h1>' . $book . ' </h1></center>
                <div class="row">
                    <div class="span6 offset3 block">
                        <figure>
                            <img src="'.$image.'" height="100" width="100" alt="Image not Available" 
                            class="img-polaroid">
                        </figure>
                        <strong>Author: </strong>' . $author . '<br>
                        <strong>Edition: </strong>' . $edition . ' <br>
                        <strong>ISSN: </strong>' . $issn . ' <br>
                        <strong>Class: </strong>' . $class . ' <br>
                        <strong>Professor: </strong>' . $professor . ' <br>
                        <strong>PRICE: $</strong>' . $price . ' <br>
                    </div>
                </div>
                <br>
                    <?php if (isset($_SESSION[\'myusername\'])) {
                    ?>
                    <div class="row">
                        <div class="span6 offset3 block center-text">
                            <center><strong>Email: </strong> <?php echo $_SESSION[\'myusername\'] ?> </center>
                        </div>
                    </div>
                    <?php } ?>
                <br>
                <div class="row">
                    <div class="span2 offset5">
                        <button class="btn btn-large btn-block btn-primary" 
                        type="button" value="LOGIN" onclick="location.href=\'http://uhbookselling.net78.net\'">Search Again</button>
                    </div>
                </div>
            </div>
            <br>

        </body>
        </html>
    ';
    
    //Write all the data to the file
    fwrite($ourFileHandle, $data);
    //then close the file
    fclose($ourFileHandle);
    header("Location: books/$ourFileName");
  
?>				